package com.server.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.commons.lang.ObjectUtils.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.date.DateType;
import com.server.date.DateUtil;
import com.server.service.ICommonDataService;
import com.server.service.IRewardService;

/**
 * 
 * @author ServerZhang
 * @date 2017年3月10日
 */
@Service("RewardService")
public class RewardService implements IRewardService {
	
	private static final String TABLE_NAME_REWARD = "reward.reward";
	private static final String TABLE_NAME_SOURCE = "reward.source";
	private static final String TABLE_NAME_REWARD_DETAIL = "reward.reward_detail";
	private static final String TABLE_NAME_EMPLOY = "reward.employ";
	private static final String VIEW_NAME_EMPLOY = "reward.v_employ_reward";
	private static final String VIEW_NAME_SOURCE = "reward.v_employ_source";
	
	private List<String> levels = new ArrayList<String>();//所有在职员工分级
	private List<List<Map<String, Object>>> levelList = new ArrayList<List<Map<String,Object>>>();//所有在职员工分级
	
	private ReadWriteLock lock = new ReentrantReadWriteLock(false);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	
	 /**
	 * 初始化员工层级数据，在工程启动时进行一次，每天23：30重新执行一次
	 * @author ServerZhang
	 * @date 2017年3月11日
	 */
	@Override
	public void loadData(){
		List<Map<String, Object>> allEmployList = this.getAllEmployList();
		List<Map<String, Object>> sourceList = this.getSourceList();
		List<Map<String, Object>> addList = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < sourceList.size(); i++) {
			Map<String, Object> source = sourceList.get(i);
			boolean addFlag = true;
			for (int j = 0; j < allEmployList.size(); j++) {
				Map<String, Object> employ = allEmployList.get(i);
				if ( employ.get("CardID").equals(source.get("CardID")) ) {
					addFlag = false;
					break;
				}
			}
			if ( addFlag ) {
				Map<String, Object> map = new HashMap<String,Object>();
				map.put("CardID", source.get("CardId"));
				map.put("Source", null);
				map.put("OrgNo", "source");
				addList.add(map);
			}
		}
		addList.addAll(allEmployList);
		//list 全部员工（包含在职，离职）外加source表中的
		this.loadLevels(addList, null);
	}
	
	public List<String> getLevels() {
		return this.levels;
	}
	public void setLevels(List<String> levels) {
		this.levels = levels;
	}
	public void addLevels(String levelStr){
		this.levels.add(levelStr);
	}
	public List<List<Map<String, Object>>> getLevelList() {
		return this.levelList;
	}
	public void setLevelList(List<List<Map<String, Object>>> levelList) {
		this.levelList = levelList;
	}
	public void addLevelList(List<Map<String, Object>> list){
		this.levelList.add(list);
	}
	
	 /**
	 * 结算返费金额并记录
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月11日
	 */
	@Override
	public Boolean reward() {
		lock.readLock().lock();
		List<Map<String, Object>> employList = this.getEmployList();
		//Map<String,Map<String, Object>> updateMap = new HashMap<String, Map<String,Object>>();//返费后需要修改的数据
		Map<String,List<Map<String, Object>>> updateMap = new HashMap<String, List<Map<String,Object>>>();//返费后需要修改的数据
//		List<Map<String, Object>> updateList = new ArrayList<Map<String,Object>>();//返费后需要修改的数据
//		List<Map<String, Object>> rewardList = new ArrayList<Map<String,Object>>();//需要写入返费表的的数据
		Iterator<Map<String, Object>> iterator = employList.iterator();
		//StringBuffer ids_by_day = new StringBuffer();//满足返费条件的所有员工id,按工作日返费
		List<String> sqls_by_day = new ArrayList<String>();//满足返费条件的所有员工,返费后update的SQL
		StringBuffer ids = new StringBuffer();//满足返费条件的所有员工id，不按工作日返费
		String dateStr = DateUtil.getDateStr(new Date(), DateType.YYYY_MM_DD_hh);
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object idObj = next.get("Id");
			Object userNameObj = next.get("UserName");
			Object rewardIdObj = next.get("RewardId");
			Object rewardTypeObj = next.get("RewardType");
			Object cardIDObj = next.get("CardID");
			Object sourceObj = next.get("Source");
			if (rewardIdObj == null || rewardTypeObj == null
					|| cardIDObj == null|| sourceObj == null
					|| idObj == null|| userNameObj == null) {
				if( cardIDObj!=null ){
					System.out.println(cardIDObj.toString()+"----员工信息不完整");
				}else{
					System.out.println("身份证号码为空----员工信息不完整");
				}
				continue;
			}
			String cardId = cardIDObj.toString();
			if( cardId.equals("620523199508252056") ){
				System.out.println("请注意倒车！");
			}
			String userName = userNameObj.toString();
			Integer workDay = (Integer)next.get("WorkDay");//要求工作天数
			Integer subWorkDay = (Integer)next.get("SubWorkDay");//二阶段返费要求工作天数
			Date createTime = (Date)next.get("EmployTime");//入职时间
			Object rewardTimeObj = next.get("RewardTime");//最近返费时间
			Float limitCount = (Float)next.get("LimitCount");//可返费次数
			Float planCount = (Float)next.get("PlanCount");//计划返费次数
			Float limitCount1 = (Float)next.get("LimitCount1");//第一阶段返费次数
			Float subCount = (Float)next.get("SubCount");//第二阶段返费次数
			Float currentDay = (Float)next.get("CurrentDay");//未返费天数,返费记录后的考勤天数
			int rewardType = Integer.parseInt(rewardTypeObj.toString());//返费类型
			//1.判断是否符合返费条件，不符合则跳出循环
			if (limitCount <= 0) {//剩余返费次数为0
				System.out.println(userName+cardId+"----剩余返费次数为0");
				continue;
			}
			if (rewardType == 4) {//按工作日计算
				if (currentDay <= 0) {//返费后考勤天数为0
					System.out.println(userName+cardId+"----返费后考勤天数为0");
					continue;
				}
				Float limitC = jdbcTemplate.queryForObject(String.format("SELECT LimitCount FROM %s where id=%s", TABLE_NAME_EMPLOY,idObj), Float.class);
				if (currentDay > limitC) {
					currentDay = limitC;
				}
				String sql_by_day = "update %s set LimitCount = %s,RewardTime='%s',CurrentDay=0 where id=%s";
				sqls_by_day.add(String.format(sql_by_day, TABLE_NAME_EMPLOY,(currentDay-limitC),dateStr,idObj));
			} else {//非按工作日计算
				if (rewardTimeObj == null) {//未曾返过费，判断是否符合条件
					if(createTime==null){
						System.out.println(userName+cardId+"----入职时间为空！");
						continue;
					}
					int days = DateUtil.daysBetween(new Date(), createTime);
					if (days < workDay) {//不满足返费条件
						System.out.println(userName+cardId+"----不满足返费条件！(工作天数小于要求满足工作天数)---已经工作天数"+days+" 要求工作天数"+workDay);
						continue;
					}
				} else {
					int days = DateUtil.daysBetween(new Date(), (Date)rewardTimeObj);
					if (rewardType == 5) {//分阶段返费
						if (planCount - limitCount < limitCount1) {//已返次数小于第一阶段次数，第一阶段返费
							if (days < workDay) {//不满足返费条件
								System.out.println(userName+cardId+"----不满足返费条件！(工作天数小于要求满足工作天数)---已经工作天数"+days+" 要求工作天数"+workDay);
								continue;
							}
						} else {//第二阶段返费
							if (days < subWorkDay) {//不满足返费条件
								System.out.println(userName+cardId+"----不满足返费条件！(工作天数小于二阶段要求满足工作天数)");
								continue;
							}
						}
					}else{
						if (days < workDay) {//不满足返费条件
							System.out.println(userName+cardId+"----不满足返费条件！(工作天数小于要求满足工作天数)---已经工作天数"+days+" 要求工作天数"+workDay);
							continue;
						}
					}
				}
				if (ids.length() > 0) {
					ids.append(",");
				}
				ids.append(idObj);
			}
			
			Float amount1 = (Float)next.get("Amount1");
			Float amount2 = (Float)next.get("Amount2");
			Float amount3 = (Float)next.get("Amount3");
			Float amount4 = (Float)next.get("Amount4");
			
			Float sub_amount1 = (Float)next.get("SubAmount1");
			Float sub_amount2 = (Float)next.get("SubAmount2");
			Float sub_amount3 = (Float)next.get("SubAmount3");
			Float sub_amount4 = (Float)next.get("SubAmount4");
			
			Float real1 = 0.0F;//实际第一级返费
			Float real2 = 0.0F;//实际第二级返费
			Float real3 = 0.0F;//实际第三级返费
			Float real4 = 0.0F;//实际第四级返费
			 
			switch (rewardType) {
			case 1:
				real1 = amount1;
				real2 = amount2;
				real3 = amount3;
				real4 = amount4;
				break;
			case 2:
				real1 = amount1*(planCount-limitCount+1);
				real2 = amount2*(planCount-limitCount+1);
				real3 = amount3*(planCount-limitCount+1);
				real4 = amount4*(planCount-limitCount+1);
				break;
			case 3:
				real1 = amount1;
				real2 = amount2;
				real3 = amount3;
				real4 = amount4;
				break;
			case 4:
				real1 = amount1*currentDay;
				real2 = amount2*currentDay;
				real3 = amount3*currentDay;
				real4 = amount4*currentDay;
				break;
			case 5:
				if (planCount - limitCount < limitCount1) {//已返次数小于第一阶段次数，第一阶段返费
					real1 = amount1;
					real2 = amount2;
					real3 = amount3;
					real4 = amount4;
				} else {//第二返费
					real1 = sub_amount1;
					real2 = sub_amount2;
					real3 = sub_amount3;
					real4 = sub_amount4;
				}
				
				break;
			default:
				break;
			}
			
			//判断该员工在层级数据中属于第几级，从0级开始
			Integer level = this.getLevel(cardId);
			if (level.equals(-1)) {//不可能的情况，该员工数据有误 
				System.out.println(userName+cardId+"----该员工数据有误 (层级关系有错误)!");
				continue;
			}
			
			String source1 = sourceObj.toString(); 
			String source2 = null;
			String source3 = null;
			String source4 = null;
			if (!real1.equals(0) && level > 0) {//一级返费
				List<Map<String, Object>> list = levelList.get(level-1);//当前员工的上一级人员
				Iterator<Map<String, Object>> iterator2 = list.iterator();
				while(iterator2.hasNext()){
					Map<String, Object> next2 = iterator2.next();
					Object cardObj = next2.get("CardID");
					if (cardObj != null && source1.equals(cardObj)) {
						Object object = next2.get("Source");
						if (object != null) {
							source2 = object.toString();
						}
						List<Map<String, Object>> sourceList = updateMap.get(source1);
						if (sourceList == null) {
							sourceList = new ArrayList<Map<String,Object>>();
						}
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("CardID", source1);
						map.put("OrgNo", next2.get("OrgNo"));
						map.put("Amount", real1);
						map.put("Amount1", real1);
						map.put("Amount2", 0.0F);
						map.put("Amount3", 0.0F);
						map.put("Amount4", 0.0F);
						map.put("Note", userName+"："+real1+"元");
						sourceList.add(map);
						updateMap.put(source1, sourceList);
						/*Map<String, Object> map = updateMap.get(source1);
						if (map == null) {
							map = new HashMap<String, Object>();
							map.put("CardID", source1);
							map.put("OrgNo", next2.get("OrgNo"));
							map.put("Amount", real1);
							map.put("Amount1", real1);
							map.put("Amount2", 0.0F);
							map.put("Amount3", 0.0F);
							map.put("Amount4", 0.0F);
							map.put("Note", userName+"："+real1+"元");
						}else {
							Float tempTotal = (Float)map.get("Amount");
							Float tempAmount = (Float)map.get("Amount1");
							map.put("Amount", tempTotal+real1);
							map.put("Amount1", tempAmount+real1);
							Object noteObj = map.get("Note");
							if (noteObj != null) {
								map.put("Note", noteObj.toString()+"；"+userName+"："+real1+"元");
							}else {
								map.put("Note", userName+"："+real1+"元");
							}
						}*/
						break;//找到上级后，跳出循环
					}
				}
			}
			if (!real2.equals(0) && source2!=null && level >= 2) {//二级返费
				List<Map<String, Object>> list = levelList.get(level-2);//当前员工的上二级人员
				Iterator<Map<String, Object>> iterator2 = list.iterator();
				while(iterator2.hasNext()){
					Map<String, Object> next2 = iterator2.next();
					Object cardObj = next2.get("CardID");
					if (cardObj != null && source2.equals(cardObj)) {
						Object object = next2.get("Source");
						if (object != null) {
							source3 = object.toString();
						}
						List<Map<String, Object>> sourceList = updateMap.get(source2);
						if (sourceList == null) {
							sourceList = new ArrayList<Map<String,Object>>();
						}
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("CardID", source2);
						map.put("OrgNo", next2.get("OrgNo"));
						map.put("Amount", real2);
						map.put("Amount2", real2);
						map.put("Amount1", 0.0F);
						map.put("Amount3", 0.0F);
						map.put("Amount4", 0.0F);
						map.put("Note", userName+"："+real2+"元");
						sourceList.add(map);
						updateMap.put(source2, sourceList);
						/*Map<String, Object> map = updateMap.get(source2);
						if (map == null) {
							map = new HashMap<String, Object>();
							map.put("CardID", source2);
							map.put("OrgNo", next2.get("OrgNo"));
							map.put("Amount", real2);
							map.put("Amount2", real2);
							map.put("Amount1", 0.0F);
							map.put("Amount3", 0.0F);
							map.put("Amount4", 0.0F);
							map.put("Note", userName+"："+real2+"元");
						}else {
							Float tempTotal = (Float)map.get("Amount");
							Float tempAmount = (Float)map.get("Amount2");
							map.put("Amount", tempTotal+real2);
							map.put("Amount2", tempAmount+real2);
							Object noteObj = map.get("Note");
							if (noteObj != null) {
								map.put("Note", noteObj.toString()+"；"+userName+"："+real2+"元");
							}else {
								map.put("Note", userName+"："+real2+"元");
							}
						}
						updateMap.put(source2, map);*/
						break;//找到上级后，跳出循环
					}
				}
			}
			if (!real3.equals(0) && source3!=null && level >= 3) {//三级返费
				List<Map<String, Object>> list = levelList.get(level-3);//当前员工的上三级人员
				Iterator<Map<String, Object>> iterator2 = list.iterator();
				while(iterator2.hasNext()){
					Map<String, Object> next2 = iterator2.next();
					Object cardObj = next2.get("CardID");
					if (cardObj != null && source3.equals(cardObj)) {
						Object object = next2.get("Source");
						if (object != null) {
							source4 = object.toString();
						}
						List<Map<String, Object>> sourceList = updateMap.get(source3);
						if (sourceList == null) {
							sourceList = new ArrayList<Map<String,Object>>();
						}
						Map<String, Object> map = new HashMap<String, Object>();
						map = new HashMap<String, Object>();
						map.put("CardID", source3);
						map.put("OrgNo", next2.get("OrgNo"));
						map.put("Amount", real3);
						map.put("Amount3", real3);
						map.put("Amount2", 0.0F);
						map.put("Amount1", 0.0F);
						map.put("Amount4", 0.0F);
						map.put("Note", userName+"："+real3+"元");
						sourceList.add(map);
						updateMap.put(source3, sourceList);
						/*Map<String, Object> map = updateMap.get(source3);
						if (map == null) {
							map = new HashMap<String, Object>();
							map.put("CardID", source3);
							map.put("OrgNo", next2.get("OrgNo"));
							map.put("Amount", real3);
							map.put("Amount3", real3);
							map.put("Amount2", 0.0F);
							map.put("Amount1", 0.0F);
							map.put("Amount4", 0.0F);
							map.put("Note", userName+"："+real3+"元");
						}else {
							Float tempTotal = (Float)map.get("Amount");
							Float tempAmount = (Float)map.get("Amount3");
							map.put("Amount", tempTotal+real3);
							map.put("Amount3", tempAmount+real3);
							Object noteObj = map.get("Note");
							if (noteObj != null) {
								map.put("Note", noteObj.toString()+"；"+userName+"："+real3+"元");
							}else {
								map.put("Note", userName+"："+real3+"元");
							}
						}
						updateMap.put(source3, map);*/
						break;//找到上级后，跳出循环
					}
				}
			}
			if (!real4.equals(0) && source4!=null && level >= 4) {//四级返费
				List<Map<String, Object>> list = levelList.get(level-4);//当前员工的上四级人员
				Iterator<Map<String, Object>> iterator2 = list.iterator();
				while(iterator2.hasNext()){
					Map<String, Object> next2 = iterator2.next();
					Object cardObj = next2.get("CardID");
					if (cardObj != null && source4.equals(cardObj)) {
						List<Map<String, Object>> sourceList = updateMap.get(source4);
						if (sourceList == null) {
							sourceList = new ArrayList<Map<String,Object>>();
						}
						Map<String, Object> map = new HashMap<String, Object>();
						map = new HashMap<String, Object>();
						map.put("CardID", source4);
						map.put("OrgNo", next2.get("OrgNo"));
						map.put("Amount", real4);
						map.put("Amount4", real4);
						map.put("Amount2", 0.0F);
						map.put("Amount3", 0.0F);
						map.put("Amount1", 0.0F);
						map.put("Note", userName+"："+real4+"元");
						sourceList.add(map);
						updateMap.put(source4, sourceList);
						
						/*Map<String, Object> map = updateMap.get(source4);
						if (map == null) {
							map = new HashMap<String, Object>();
							map.put("CardID", source4);
							map.put("OrgNo", next2.get("OrgNo"));
							map.put("Amount", real4);
							map.put("Amount4", real4);
							map.put("Amount2", 0.0F);
							map.put("Amount3", 0.0F);
							map.put("Amount1", 0.0F);
							map.put("Note", userName+"："+real4+"元");
						}else {
							Float tempTotal = (Float)map.get("Amount");
							Float tempAmount = (Float)map.get("Amount4");
							map.put("Amount", tempTotal+real4);
							map.put("Amount4", tempAmount+real4);
							Object noteObj = map.get("Note");
							if (noteObj != null) {
								map.put("Note", noteObj.toString()+"；"+userName+"："+real4+"元");
							}else {
								map.put("Note", userName+"："+real4+"元");
							}
						}
						updateMap.put(source4, map);*/
						break;//找到上级后，跳出循环
					}
				}
			}
		}//遍历所有员工，对他的一、二、三、四级代理人进行返费计算
		if (updateMap.size() <= 0) {
			return false;
		}
		//1.记录结算结果到返费表
		Boolean addOrUpdate = this.addOrUpdate(updateMap);
		//2.将当前已计算返费的员工剩余返费次数减1,短期工减去考勤天数
		
		if (ids.length() > 0) {
			String sql1 = "update %s set LimitCount = LimitCount - 1,RewardTime='%s' where id in(%s)";
			int update1 = jdbcTemplate.update(String.format(sql1, TABLE_NAME_EMPLOY,dateStr,ids.toString()));
		}
		
		Iterator<String> iterator2 = sqls_by_day.iterator();
		String[] sql2 = new String[sqls_by_day.size()];
		int index = 0;
		while(iterator2.hasNext()){
			sql2[index++] = iterator2.next();
		}
		if (sql2.length > 0) {
			int[] batchUpdate = jdbcTemplate.batchUpdate(sql2);
		}
		
		//重新获取数据
		this.loadData();
		
		lock.readLock().unlock();
		
		return addOrUpdate;
	}
	public Boolean addOrUpdate(Map<String, List<Map<String, Object>>> inputMap){
		
		List<Map<String, Object>> detailList = new ArrayList<Map<String,Object>>();//返费详细记录
		Set<String> mapKeys = inputMap.keySet();
		for (String key : mapKeys) {
			detailList.addAll(inputMap.get(key));
		}
		
		commonDataService.addBatch("system", TABLE_NAME_REWARD_DETAIL, detailList);
		
		List<Map<String, Object>> addList = new ArrayList<Map<String,Object>>();//新增记录
		List<Map<String, Object>> updateList = new ArrayList<Map<String,Object>>();//修改记录
		Map<String, List<Map<String, Object>>> tempMap = new HashMap<String, List<Map<String, Object>>>();
		tempMap.putAll(inputMap);
		String sql = "select * from %s where state=0";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, TABLE_NAME_REWARD));
		Iterator<Map<String, Object>> iterator = list.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object cardIDObj = next.get("CardID");
			Object idObj = next.get("Id");
			if (cardIDObj == null || idObj == null) {
				continue;
			}
			String cardId = cardIDObj.toString();
			List<Map<String, Object>> list2 = inputMap.get(cardId);
			if( list2==null || list.size()<=0 ){
				continue;
			}
			Iterator<Map<String, Object>> iterator2 = list2.iterator();
			Map<String, Object> temp = new HashMap<String, Object>();
			temp.put("Id", idObj);
			temp.put("Amount", next.get("Amount"));
			temp.put("Amount1", next.get("Amount1"));
			temp.put("Amount2", next.get("Amount2"));
			temp.put("Amount3", next.get("Amount3"));
			temp.put("Amount4", next.get("Amount4"));
			while(iterator2.hasNext()){//需要修改的记录
				Map<String, Object> map = iterator2.next();
				Float amount = (Float)map.get("Amount");
				Float amount1 = (Float)map.get("Amount1");
				Float amount2 = (Float)map.get("Amount2");
				Float amount3 = (Float)map.get("Amount3");
				Float amount4 = (Float)map.get("Amount4");
				
				Float amount_old = (Float)temp.get("Amount");
				Float amount1_old = (Float)temp.get("Amount1");
				Float amount2_old = (Float)temp.get("Amount2");
				Float amount3_old = (Float)temp.get("Amount3");
				Float amount4_old = (Float)temp.get("Amount4");
				
				temp.put("Amount", amount+amount_old);
				temp.put("Amount1", amount1+amount1_old);
				temp.put("Amount2", amount2+amount2_old);
				temp.put("Amount3", amount3+amount3_old);
				temp.put("Amount4", amount4+amount4_old);
			}
			updateList.add(temp);
			
			inputMap.remove(cardId);//移除已计算的数据
			
			/*if (map!=null && map.size()>0) {//需要修改的记录
				Float amount = (Float)map.get("Amount");
				Float amount1 = (Float)map.get("Amount1");
				Float amount2 = (Float)map.get("Amount2");
				Float amount3 = (Float)map.get("Amount3");
				Float amount4 = (Float)map.get("Amount4");
				
				Float amount_old = (Float)next.get("Amount");
				Float amount1_old = (Float)next.get("Amount1");
				Float amount2_old = (Float)next.get("Amount2");
				Float amount3_old = (Float)next.get("Amount3");
				Float amount4_old = (Float)next.get("Amount4");
				
				inputMap.remove(cardId);//移除已计算的数据
				
				map.put("Id", idObj);
				map.put("Amount", amount+amount_old);
				map.put("Amount1", amount1+amount1_old);
				map.put("Amount2", amount2+amount2_old);
				map.put("Amount3", amount3+amount3_old);
				map.put("Amount4", amount4+amount4_old);
				updateList.add(map);
			}*/
		}
		Set<String> keySet = inputMap.keySet();
		for (String key : keySet) {
			addList.addAll(inputMap.get(key));
		}
		Boolean isSuccess = true;
		//1.数据更新
		if (updateList.size()>0) {
			isSuccess = commonDataService.updateBatch("system", TABLE_NAME_REWARD, "Id", updateList);
		}
		//2.数据添加
		if (addList.size()>0) {
			isSuccess = commonDataService.addBatch("system", TABLE_NAME_REWARD, addList);
		}
		
		return isSuccess;
	}
	
	 /**
	 * 获取所有可能需要向代理人返费的员工列表
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月10日
	 */
	public List<Map<String, Object>> getEmployList(){
		String sql = "select * from %s";
		return jdbcTemplate.queryForList(String.format(sql, VIEW_NAME_EMPLOY));
	}
	 /**
	 * 获取所有在职人员列表
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月10日
	 */
	public List<Map<String, Object>> getSourceList(){
		String sql = "select * from %s ";
		return jdbcTemplate.queryForList(String.format(sql, TABLE_NAME_SOURCE));
	}
	
	public List<Map<String, Object>> getAllEmployList(){
		String sql = "select * from %s order by Source asc,CreateTime asc";
		return jdbcTemplate.queryForList(String.format(sql, TABLE_NAME_EMPLOY));
	}
	 /**
	 * 将员工列表处理成为层级列表
	 * @param list
	 * @param parents
	 * @author ServerZhang
	 * @date 2017年3月11日
	 */
	public void loadLevels(List<Map<String, Object>> list,String parents){
		List<Map<String, Object>> tempList = new ArrayList<Map<String,Object>>();
		tempList.addAll(list);
		Iterator<Map<String, Object>> iterator = list.iterator();
		StringBuffer sBuffer = new StringBuffer();
		List<Map<String, Object>> leveList = new ArrayList<Map<String,Object>>();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object sourceObj = next.get("Source");
			Object idObj = next.get("CardID");
			if (idObj == null) {
				continue;
			}
			if (sourceObj == null) {//第一级
				if (sBuffer.length() > 0) {
					sBuffer.append(",");
				}
				sBuffer.append(idObj);
				leveList.add(next);
				tempList.remove(next);//移除当前Map
			}else if (parents == null) {//非第一级但当前只要第一级
				break;//效率高，但必须保证所有第一级数据排在最前
				//continue;//安全稳定，但效率明显降低
			} else {//非第一级
				if (parents.contains(sourceObj.toString())) {
					if (sBuffer.length() > 0) {
						sBuffer.append(",");
					}
					sBuffer.append(idObj);
					leveList.add(next);
					tempList.remove(next);//移除当前Map
				}
			}
		}
		if (sBuffer.length() > 0) {
			this.addLevels(sBuffer.toString());
			this.addLevelList(leveList);
		} else {//此种情况不存在
			return;
		}
		if (tempList.size() > 0) {
			loadLevels(tempList, sBuffer.toString());
		}
	}
	 /**
	 * 获取员工在整个系统中处于的层级
	 * @param cardId
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月11日
	 */
	public Integer getLevel(String cardId){
		for(int i=0,length=levels.size(); i<length; i++){
			if (levels.get(i).contains(cardId)) {
				return i;
			}
		}
		return -1;//错误
	}
	
	
}
